﻿
using System.Text;
namespace CommandLib
{
    /// <summary>
    /// Base64转换类
    /// </summary>
    public sealed class Base64Helper
    {
        /// <summary>
        /// Base64加密，采用utf8编码方式加密
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的字符串</returns>
        public static string Base64Encode(string source)
        {
            return Base64Encode(Encoding.UTF8, source);
        }

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encodeType">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string Base64Encode(Encoding encodeType, string source)
        {
            string encode = string.Empty;
            byte[] bytes = encodeType.GetBytes(source);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }

        /// <summary>
        /// Base64解密，采用utf8编码方式解密
        /// </summary>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(string result)
        {
            return Base64Decode(Encoding.UTF8, result);
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="encodeType">解密采用的编码方式，注意和加密时采用的方式一致</param>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string Base64Decode(Encoding encodeType, string result)
        {
            string decode = string.Empty;
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encodeType.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }

        /// <summary>
        /// 文件转base64
        /// </summary>
        /// <returns>base64字符串</returns>
        public static string FileToBase64String(string fileroot)
        {
            string base64Str = "";
            try
            {
                //文件路径
                using (var fsForRead = new FileStream(fileroot, FileMode.Open))
                {
                    fsForRead.Seek(0, SeekOrigin.Begin);
                    byte[] bs = new byte[fsForRead.Length];
                    int log = Convert.ToInt32(fsForRead.Length);                   
                    fsForRead.Read(bs, 0, log);
                    base64Str = Convert.ToBase64String(bs);
                }           

                return base64Str;
            }
            catch (Exception)
            {
                throw;
            }           
        }

        /// <summary>
        /// Base64字符串转文件并保存
        /// </summary>
        /// <param name="base64String">base64字符串</param>
        /// <param name="fileName">保存的文件名</param>
        /// <returns>是否转换并保存成功</returns>
        public static bool Base64StringToFile(string base64String, string fileName)
        {
            bool opResult = false;
            try
            {
                using (var stream = new MemoryStream(Convert.FromBase64String(base64String)))
                {
                    FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                    byte[] b = stream.ToArray();
                    fs.Write(b, 0, b.Length);
                    fs.Close();
                }
                opResult = true;
            }
            catch (Exception)
            {
                throw;
            }
            return opResult;
        }

    }
}
